# -*- coding:utf-8 -*-
"""
配置（定时读取配置）文件
"""

import json
from sys import maxsize
import threading
import time
import datetime
from threading import Timer
import re
from queue import Queue,LifoQueue,PriorityQueue
# from utilities.log_help import LogHelper,TraceHelper,DebugHelper
from log_help import TraceHelper

class SingletonGlobalShareVariable(object):
    _instance_lock = threading.Lock()

    def __init__(self, *args, **kwargs):
        # time.sleep(1)
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            with SingletonGlobalShareVariable._instance_lock:
                if not hasattr(cls, '_instance'):
                    SingletonGlobalShareVariable._instance = super().__new__(cls)
        return SingletonGlobalShareVariable._instance





class GlobalShareVariable(SingletonGlobalShareVariable):

    def __init__(self):
        if not hasattr(self, '_instance_init'):
            self._instance_init = True
            self.message_id = 'null'
            self.exception_auto_trace_flag = False
            self.trace_queue = Queue(maxsize=1000)
            self.version = None

    def trace_log_print(self):
        """
        清空trace_queue并按照要求判断是否打印.
        :param self.trace_queue:
        :return: None

        """
        while not self.trace_queue.empty():
            val = self.trace_queue.get_nowait()  # 拿出已经生产好的数据
            if self.exception_auto_trace_flag:
                l2 = TraceHelper(val[0])
                l2.log(transaction_id=val[1], start_time=val[2], end_time=val[3], run_time=val[4],
                        in_parameter = val[5],out_parameter = val[6])
            self.trace_queue.task_done() # 告诉队列有关这个数据的任务已经处理完成


    def update(self, update_fields):
        """
        更新update fields.
        :param update_fields:
        :return: None

        """

        for k, v in update_fields.items():
            setattr(self, k, v)

Global_share_variable = GlobalShareVariable()

# config1 = Config()

if __name__ == "__main__":
    # config.real_time_update()
    # config.loads("config.json")

    '''
    使用方法，
    1、应用本模块
    from manual_config import Manual_config
    2、获取参数
    a = getattr(Manual_config,"config","g1")

    '''

    a = getattr(Global_share_variable,"config","g1")
    b = getattr(Global_share_variable,"bb","g")
    print(a,type(a))
    print(b,type(b))

    # a1 = getattr(config1,"config","g")
    # b1 = getattr(config1,"bb","g")
    # print(a1,type(a1))
    # print(b1,type(b1))

    while True:
        a = getattr(Global_share_variable,"config","g1")
        b = getattr(Global_share_variable,"bb","g")
        print(a,type(a))
        print(b,type(b))

        time.sleep(15)